package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jcr.query.Query;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.jsonUtils.AjaxResult;
import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.CommonDateUtil;
import com.thinkingstar.iads.cs.sys.entity.SysUser;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignConstant;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignConstantVersion;

/**
 * @ClassName: DaoDcProjectDesignConstant 
 * @Description:数据中心_常量_常量库维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcProjectDesignConstant extends EntityRepositoryServiceImpl<DcProjectDesignConstant>{

	public DaoDcProjectDesignConstant(Dao dao) {
		super(dao);
	}
	
	
	//常量集查询
	public QueryResult findConstantInfoListById(NutMap nutMap){
		List<Map> list = null;
		Pager pager = null;
		String find_sql = " select * , case standard_type when '0' then '常量' else '代码' end standard_type_jc "
				+ " from dc_constant_data where del_flag = '0' and constant_version_id = @version_id "
				+ " order by create_time desc ";
		Sql sql = Sqls.create(find_sql);
		sql.params().set("version_id", nutMap.getString("constant_version_id"));
		if(nutMap.get("page_num") != null && nutMap.get("page_size") != null){
			pager = new Pager(nutMap.getInt("page_num"),nutMap.getInt("page_size"));
			pager.setRecordCount(this.findConstantNumById(nutMap));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
			    List<Map<String,Object>> list = new ArrayList<>();
			    while (rs.next()){
			        Map<String,Object> map = new HashMap<>();
			        ResultSetMetaData metaData = rs.getMetaData();
			        int count = metaData.getColumnCount();
			        for (int i = count; i > 0; i--) {
			            String filed = metaData.getColumnName(i);
			            map.put(filed, rs.getString(filed));
			        }
			        list.add(map);
			    }
			    return list;
			}
		});
		dao().execute(sql);
		list = sql.getList(Map.class);
		return new QueryResult(list,pager);
	}
	
	public int findConstantNumById(NutMap nutMap){
		String find_sql = " select count(id) num "
				+ " from dc_constant_data where del_flag = '0' and constant_version_id = @version_id "
				+ " order by create_time desc ";
		Sql sql = Sqls.create(find_sql);
		sql.params().set("version_id", nutMap.getString("constant_version_id"));
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
			    int num = 0;
				while (rs.next()){
			    	num = rs.getInt("num");
			    }
			    return num;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}
	
	//添加常量集
	public AjaxResult updateConstantInfo(DcProjectDesignConstant dcProjectDesignConstant , HttpSession session){
		if(dcProjectDesignConstant == null){
			return AjaxResult.error("更新失败!");
		}
		SysUser user = (SysUser) session.getAttribute("userSession");
		int i = 0;
		String id = dcProjectDesignConstant.getId();
		if(StringUtils.isBlank(id)){
			dcProjectDesignConstant.setCreate_time(CommonDateUtil.getDateTime());
			dcProjectDesignConstant.setCode_create_user(user.getLoginname());
			Object object = insert(dcProjectDesignConstant);
			if(object != null){
				i = 1;
			}
		}else{
			DcProjectDesignConstant update_obj = fetch(id);
			update_obj.setCode_cname(dcProjectDesignConstant.getCode_cname());
			update_obj.setCode_create_user(user.getLoginname());
			update_obj.setCode_name(dcProjectDesignConstant.getCode_name());
			update_obj.setCode_value(dcProjectDesignConstant.getCode_value());
			update_obj.setCode_value_unit(dcProjectDesignConstant.getCode_value_unit());
			update_obj.setCode_default_value(dcProjectDesignConstant.getCode_default_value());
			update_obj.setHas_children(dcProjectDesignConstant.getHas_children());
			update_obj.setUpdate_time(CommonDateUtil.getDateTime());
			i = update(update_obj);
		}
		return i==1?AjaxResult.success("更新成功!"):AjaxResult.error("更新失败!");
	}
	
	//查询上级数据项
	public List<DcProjectDesignConstant> findParentConstant(String constant_version_id){
		List <DcProjectDesignConstant> result_list = null;
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false).and("constant_version_id", "=", constant_version_id).and("has_children", "=", true).and("standard_type", "=", "0");
		result_list = query(criteria);
		return result_list;
	}
}
